/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.bjj;

import com.bff.startup.StartUpProperties;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bill
 */
public abstract class AbstractJukeboxProperties {

    private static final String PROP_NOTIFIED = "notified.properties";
    private static final String PROP_FILE_DIR = System.getProperty("user.home")
            + System.getProperty("file.separator")
            + ".bjj";
    private static final String PROP_NOTIFIED_FILE_NAME = PROP_FILE_DIR
            + System.getProperty("file.separator")
            + PROP_NOTIFIED;
    private static final String URL_VERSION_AVAILABLE = "http://www.thejavashop.net/bjj/version.txt";
    private static final String PROP_UPDATE_NOTIFIED = "notified" + StartUpProperties.getVersion();
    private String updateURL;
    private boolean versionNotified;
    private boolean trayMin;
    private boolean trayAlways;
    private boolean groupByFolder;
    //props file entries
    protected static final String PROP_MIN_TRAY = "min.tray";
    protected static final String PROP_ALWAYS_TRAY = "always.tray";
    protected static final String PROP_GROUP_FOLDER = "group.folder";

    public AbstractJukeboxProperties() {
        FileInputStream inFile = null;
        try {
            File f = new File(PROP_NOTIFIED_FILE_NAME);

            if (f.exists()) {
                inFile = new FileInputStream(PROP_NOTIFIED_FILE_NAME);
                Properties props = new Properties();
                props.load(inFile);

                setVersionNotified(Boolean.parseBoolean(props.getProperty(PROP_UPDATE_NOTIFIED, "false")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inFile != null) {
                    inFile.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(AbstractJukeboxProperties.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String versionAvailable() throws IOException {
        String v = null;
        InputStream inStream = null;
        try {
            URL url = new URL(URL_VERSION_AVAILABLE);
            URLConnection urlConnection = url.openConnection();
            inStream = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));

            String inputLine = null;
            while ((inputLine = reader.readLine()) != null) {
                if (inputLine.startsWith("version")) {
                    v = inputLine.split("=")[1];
                }
                if (inputLine.startsWith("updateURL")) {
                    setUpdateURL(inputLine.replace("updateURL=", ""));
                }
            }

        } catch (MalformedURLException me) {
            throw me;
        } finally {
            inStream.close();
        }

        return v;
    }

    public void notifiedOfUpdate() {
        setVersionNotified(true);
        Properties versionProps = new Properties();
        versionProps.setProperty(PROP_UPDATE_NOTIFIED, "true");

        FileOutputStream outFile = null;
        try {
            outFile = new FileOutputStream(PROP_NOTIFIED_FILE_NAME);
            versionProps.store(outFile, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getVersion() {
        return StartUpProperties.getVersion();
    }

    public boolean isDeveloper() {
        return StartUpProperties.getInstance().isDeveloper();
    }

    public boolean isSnapShot() {
        return StartUpProperties.getInstance().isSnapshot();
    }

    public void setUpdateURL(String URL) {
        updateURL = URL;
    }

    public String getUpdateURL() {
        return updateURL;
    }

    public boolean isVersionNotified() {
        return versionNotified;
    }

    public final void setVersionNotified(boolean notified) {
        versionNotified = notified;
    }


    public boolean isTrayMin() {
        return trayMin;
    }

    public void setTrayMin(boolean minTray) {
        this.trayMin = minTray;
    }

    /**
     * @return the trayAlways
     */
    public boolean isTrayAlways() {
        return trayAlways;
    }

    /**
     * @param trayAlways the trayAlways to set
     */
    public void setTrayAlways(boolean trayAlways) {
        this.trayAlways = trayAlways;
    }

    /**
     * @return the groupByFolder
     */
    public boolean isGroupByFolder() {
        return groupByFolder;
    }

    /**
     * @param groupByFolder the groupByFolder to set
     */
    public void setGroupByFolder(boolean groupByFolder) {
        this.groupByFolder = groupByFolder;
    }
}
